// 将多层路径拼接成完整的路径，并在适当位置加上分隔符
function join() {
  if (arguments.length == 0) {
    return '.';
  }
  var sep = arguments[0].indexOf('/') > -1 ? '/' : '\\'
  var joined;
  for (var i = 0; i < arguments.length; ++i) {
    var arg = arguments[i];
    if (arg === undefined) continue;
    if (arg.length > 0) {
      if (joined === undefined) {
        joined = arg;
      } else if (arg.startsWith(sep) || joined.endsWith(sep)) {
        if (arg.startsWith(sep) && joined.endsWith(sep)) {
          joined += arg.substr(sep.length);
        } else {
          joined += arg;
        }
      } else {
        joined += sep + arg;
      }
    }
  }
  return joined;
}

function getExt(path) {
  let parts = path.split('/')
  let last = parts.length > 0 ? parts[parts.length - 1] : path;
  let idx = last.lastIndexOf('.');
  if (idx >= 0) {
    return last.substr(idx+1);
  } else {
    return "";
  }
}

function getRoot(path) {
  let i = path.indexOf('/')
  if (i < 0) {
    return path;
  } else if (i == 0) {
    let j = path.indexOf('/', i+1);
    if (j < 0) {
      return path;
    } else {
      return path.substr(0, j);
    }
  } else {
    return path.substr(0, i);
  }
}

export default {
  join: join,
  getExt: getExt,
  getRoot
}